package com.okaysoft.ofb

import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class ClientController {

    def sysService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {

    }

    def treeJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page') : 0
        params.offset = (params.page - 1) * params.max
        def tj = sysService.clientTreeJson(params) as JSON
        render(tj.toString())
    }

    def treeJson1() {
        def tj = sysService.clientTreeJson_1(null) as JSON
        render(tj.toString())
    }
    
    def getClientUserForJson(){
        def cu =  ClientUser.get(params.id as Long)
        def types = cu.clientUserTypes
        types = types.collect {t->
            t.type.label
        }
        render(([id:cu.id,name:cu.name,vip:cu.vip,type:types?.join(",")] as JSON)as String)
    }

    def clientUserTreeJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page') : 0
        params.offset = (params.page - 1) * params.max

        def cus = sysService.clientUserTreeJson(params)
        render(cus as JSON)
    }

    def listJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page') : 0
        params.offset = (params.page - 1) * params.max
        def cs = []
        if (!params.id) {
            cs = Client.findAllByParentIdIsNull()
        }else{
            cs = Client.findAllByParentId(params.id)
        }
        render([rows:cs,total:cs.size()] as JSON)
    }

    def save() {
        def clientInstance
        if (params.id) {
            clientInstance = Client.get(params.id)
        }
        if (!clientInstance) {
            clientInstance = new Client()
        }
        clientInstance.properties = params

        if (!clientInstance.save(flush: true)) {
            def failure = [status: 0,msg:message(code: 'default.save.failure.message', default: 'Save failure')] as JSON
            render(failure.toString())
            return
        }

        def succ = [status: 1,msg:message(code: 'default.save.success.message', default: 'Save success')] as JSON
        render(succ.toString())
    }

    def saveUser() {
        def clientInstance
        if (params.id) {
            clientInstance = ClientUser.get(params.id)
        }
        if (!clientInstance) {
            clientInstance = new ClientUser()
        }
        clientInstance.properties = params
        if (!sysService.saveClientUser(params.type,clientInstance)) {
            log.error(clientInstance.errors)
            def failure = [status: 0,msg:message(code: 'default.save.failure.message', default: 'Save failure')] as JSON
            render(failure.toString())
            return
        }

        def succ = [status: 1,msg:message(code: 'default.save.success.message', default: 'Save success')] as JSON
        render(succ.toString())
    }

    def show() {
        def clientInstance = Client.get(params.id)
        if (!clientInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])

        }

        [clientInstance: clientInstance]
    }

    def delete() {
        def clientInstance = Client.get(params.id)
        if (!clientInstance) {
            def notfound = [status: 0, msg:  message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])] as JSON
            render(notfound.toString())
            return
        }

        try {
            clientInstance.delete(flush: true)
            def succ = [status: 1, msg:  message(code: 'default.deleted.message', args: [message(code: 'client.label', default: 'Client'), params.id])] as JSON
            render(succ.toString())

        }
        catch (DataIntegrityViolationException e) {
            def failure = [status: 0, msg:  message(code: 'default.not.deleted.message', args: [message(code: 'client.label', default: 'Client'), params.id])] as JSON
            render(failure.toString())

        }
    }

    def userDelete() {
        def clientInstance = ClientUser.get(params.id)
        if (!clientInstance) {
            def notfound = [status: 0, msg:  message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])] as JSON
            render(notfound.toString())
            return
        }

        try {
            clientInstance.delete(flush: true)
            def succ = [status: 1, msg:  message(code: 'default.deleted.message', args: [message(code: 'client.label', default: 'Client'), params.id])] as JSON
            render(succ.toString())

        }
        catch (DataIntegrityViolationException e) {
            def failure = [status: 0, msg:  message(code: 'default.not.deleted.message', args: [message(code: 'client.label', default: 'Client'), params.id])] as JSON
            render(failure.toString())

        }
    }
    
    def router(){
        def cuId = params.clientUserId
        if (!cuId){
            render(view: "/device/route",model: [routes:[]])
            return;
        }
        def clientUser = ClientUser.get(cuId as Long)
        if (!clientUser){
            render(view: "/device/route",model: [routes:[]])
            return;
        }
        def terminalClientUser = TerminalClientUser.findByClientUser(clientUser)
        if (!terminalClientUser){
            render(view: "/device/route",model: [routes:[]])
            return;
        }
        def terminal = terminalClientUser?.terminal
        if (!terminal){
            render(view: "/device/route",model: [routes:[]])
            return;
        }
        def device = terminal?.device
        if (!device){
            render(view: "/device/route",model: [routes:[]])
            return;
        }
        def list = sysService.getUpNetRouter(terminal,true)
        def downList = sysService.getDowngetNetRouter(terminal)
        [routes:downList+list]
        render(view: "/device/route",model: [routes:downList+list])
    }
}
